package com.seguriapp.complaints.gwt.client.activity;

import java.util.ArrayList;
import java.util.List;

import net.customware.gwt.dispatch.client.DispatchAsync;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.seguriapp.common.gwt.client.AbstractSeguriAppActivity;
import com.seguriapp.common.gwt.client.AbstractSeguriAppPlace;
import com.seguriapp.common.gwt.client.SeguriAppCallback;
import com.seguriapp.common.gwt.client.SeguriAppClientSession;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.complaints.gwt.shared.GetMapDefaultValuesAction;
import com.seguriapp.complaints.gwt.shared.GetMapDefaultValuesResult;
import com.seguriapp.complaints.gwt.shared.GetPoliceStationListAction;
import com.seguriapp.complaints.gwt.shared.GetPoliceStationListResult;
import com.seguriapp.complaints.gwt.shared.SaveComplaintDataAction;
import com.seguriapp.complaints.gwt.shared.SaveComplaintDataResult;
import com.seguriapp.complaints.gwt.shared.SaveComplaintMediaDataAction;
import com.seguriapp.complaints.gwt.shared.SaveComplaintMediaDataResult;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeCategoryListAction;
import com.seguriapp.crimeactivity.gwt.shared.GetCrimeCategoryListResult;
import com.seguriapp.entities.Complaint;
import com.seguriapp.entities.CrimeCategory;
import com.seguriapp.entities.MediaContent;
import com.seguriapp.entities.SeguriAppEntity;

public class RegisterComplaintActivity extends AbstractSeguriAppActivity {

  private final Display display;
  private List<MediaContent> mediaList = new ArrayList<MediaContent>();
  private List<MediaContent> mediaDeletedList = new ArrayList<MediaContent>();

  public RegisterComplaintActivity(final DispatchAsync dispatchAsync, final EventBus eventBus, final PlaceController placeController,
      final Display display, final AbstractSeguriAppPlace place, final SeguriAppClientSession clientSession) {
    super(dispatchAsync, eventBus, placeController, place, clientSession);
    display.setPresenter(this);
    this.display = display;
  }

  @Override
  protected void onStart(final AcceptsOneWidget widget, final EventBus eventBus) {
    widget.setWidget(display.asWidget());
    display.cleanFormFields();
    display.setPanelsVisibility(true, false, false);
    mediaList = new ArrayList<MediaContent>();
    mediaDeletedList = new ArrayList<MediaContent>();
    getCategoryListFromServer();
    getPoliceStationListFromServer();
    getMapDefaultValues();
  }

  public void saveComplaint(final Complaint complaint, final boolean addMedia) {
    final SaveComplaintDataAction action = new SaveComplaintDataAction();
    action.setComplaint(complaint);
    dispatchAsync.execute(action, new SeguriAppCallback<SaveComplaintDataResult>() {

      public void onSuccess(final SaveComplaintDataResult result) {
        display.showRegisteredComplaint(result.getComplaintId(), addMedia);
      }

      public void onFailure(Throwable e) {
        display.showRegisteredComplaint(null, addMedia);
      }
    });
  }

  public void addFileToList(final MediaContent media) {
    mediaList.add(media);
  }

  public void saveComplaintMedia(final Long complaintId) {
    final SaveComplaintMediaDataAction action = new SaveComplaintMediaDataAction();
    action.setComplaintId(complaintId);
    action.setMediaList(mediaList);
    action.setMediaDeletedList(mediaDeletedList);
    dispatchAsync.execute(action , new SeguriAppCallback<SaveComplaintMediaDataResult>() {

      public void onSuccess(final SaveComplaintMediaDataResult result) {
        display.showRegisteredComplaintMedia(result.isSuccess());
      }

      public void onFailure(Throwable e) {
        display.showRegisteredComplaintMedia(false);
      }
    });
  }

  public void removeMedia(final String name) {
    int index = -1;
    for (final MediaContent media : mediaList) {
      if (media.getContent().equalsIgnoreCase(name)) {
        index = mediaList.indexOf(media);
        break;
      }
    }
    if (index >= 0) {
      mediaDeletedList.add(mediaList.get(index));
      mediaList.remove(index);
      display.removePreviewImageAndLink(name);
    }
  }

  private void getMapDefaultValues() {
    dispatchAsync.execute(new GetMapDefaultValuesAction(), new SeguriAppCallback<GetMapDefaultValuesResult>() {

      public void onSuccess(final GetMapDefaultValuesResult result) {
        display.initializeMapComponents(result.getDefaultLatitude(), result.getDefaultLongitude(), result.getDefaultZoom());
      }

    });
  }

  private void getCategoryListFromServer() {
    dispatchAsync.execute(new GetCrimeCategoryListAction(), new SeguriAppCallback<GetCrimeCategoryListResult>() {

      public void onSuccess(final GetCrimeCategoryListResult result) {
        display.setCrimeCategoryList(result.getCrimeCategoryList());
      }

    });
  }

  private void getPoliceStationListFromServer() {
    dispatchAsync.execute(new GetPoliceStationListAction(), new SeguriAppCallback<GetPoliceStationListResult>() {

      public void onSuccess(final GetPoliceStationListResult result) {
        display.setPoliceStationList(result.getPoliceStationList());
      }

    });
  }

  public interface Display extends SeguriAppDisplay {

    /**
     * setea el presenter del activity
     */
    void setPresenter(RegisterComplaintActivity presenter);

    void showRegisteredComplaintMedia(boolean success);

    void removePreviewImageAndLink(String name);

    void setPanelsVisibility(boolean b, boolean c, boolean d);

    void showRegisteredComplaint(Long complaintId, boolean addMedia);

    void cleanFormFields();

    void initializeMapComponents(double defaultLatitude, double defaultLongitude, int defaultZoom);

    void setPoliceStationList(List<SeguriAppEntity> policeStationList);

    void setCrimeCategoryList(List<CrimeCategory> crimeCategoryList);

  }

}
